var teacherRuleMap = new HashMap();
var currentPage_teacher = 1;
var pageSize_teacher = 10;
var totalCount_teacher = 0;

function teacherUnSchedule() {
    // 老师不排课时间
    getTeacherRule();
    //动态构建左侧教师
    setTeacherTable();
}

function getTeacherRule() {
    jQuery.ajax({
        type: "GET",
        url: "getUnScheduleTeacherTime?taskId=" + taskId,
        cache: false,
        async: false,
        success: function (msg) {
            var message = JSON.parse(msg);
            var retData = message.data;
            if (retData != null) {
                var length = retData.length;
                for (var i = 0; i < length; i++) {
                    teacherRuleMap.put(retData[i].id, retData[i].time.split(','));
                }
            }
        }
    });
}

function setTeacherTable() {
    var teacherName = $("#teacherNameInput").val();
    $("#currentPage_teacher").text(currentPage_teacher);
    var postData = {
        "taskId": taskId,
        "teacherName": encodeURI(teacherName),
        "pageIndex": currentPage_teacher,
        "pageSize": pageSize_teacher
    };
    jQuery.ajax({
        url: "getTeacherCourseList",
        data: postData,
        cache: false,
        async: false,
        success: function (msg) {
            var ret = JSON.parse(msg);
            var retData = ret.data;
            if (retData != null) {
                var length = retData.length;
                var root = $("#mainNav_" + unScheduleRuleType).find("table tbody").eq(0);
                var rootHtml = new Array();
                if (length > 0) {
                    $("#pageUl_teacher").show();
                    var totalCount = 0;
                    for (var i = 0; i < length; i++) {
                        var teacher = retData[i];
                        rootHtml.push("<tr id='a_teacher_" + teacher.teacherId + "' onclick='javascript:getTeacherRuleById(" + teacher.teacherId + ")'>");
                        rootHtml.push("<td>" + teacher.teacherName + "</td>");
                        rootHtml.push("<td>" + teacher.courseName + "</td>");
                        rootHtml.push("</tr>");
                        if (i == 0) {
                            totalCount_teacher = ret.totalCount; //parseInt(teacher.scheduledLessons);
                            if (totalCount_teacher % pageSize_teacher == 0) {
                                $("#totalPage_teacher").text(parseInt(totalCount_teacher / pageSize_teacher));
                            } else {
                                $("#totalPage_teacher").text(parseInt(totalCount_teacher / pageSize_teacher) + 1);
                            }
                        }
                    }
                } else {
                    $("#pageUl_teacher").hide();
                }
                root.html(rootHtml.join(""));
            }
        }
    });
}

/*点击搜索教师信息*/
function searchTeacher() {
    //清空表格
    clearTimeTable();
    //设置分页信息
    currentPage_teacher = 1;
    //清除选中节点
    selectedNodeType = "";
    selectedNodeValue = "";
    setTeacherTable();
}

/*在查询框中按下enter键*/
function searchTeacherByEnterKey() {
    var event = arguments.callee.caller.arguments[0] || window.event;//消除浏览器差异
    if (event.keyCode == 13) {
        searchTeacher();
    }
}

/*点击左侧教师获取教师对应的不排课位置*/
function getTeacherRuleById(teacherId) {
    clearTimeTable();
    $("tr[id*='a_']").removeClass('selected_node');
    $("#a_teacher_" + teacherId).addClass('selected_node');

    selectedNodeType = "teacher";
    selectedNodeValue = teacherId;
    var teacherTimeArr = teacherRuleMap.get(teacherId);
    if (teacherTimeArr != null) {
        var length = teacherTimeArr.length;
        for (var i = 0; i < length; i++) {
            $("#td_" + teacherTimeArr[i]).html("不排课");
        }
    }
    //show tags
    getTeacherTagById(teacherId);
}

/*选中左侧教师再点击右侧表格设置教师不排课位置*/
function setTeacherRule(teacherId, timeIndex, isUnSchedule) {
    var teacherTimeArr = teacherRuleMap.get(teacherId);
    if (teacherTimeArr == null) {
        if (isUnSchedule == true) {
            var arr = new Array();
            arr.push(timeIndex);
            teacherRuleMap.put(teacherId, arr);
        }
    } else {
        if (isUnSchedule == true) {
            if (!teacherTimeArr.contains(timeIndex)) {
                teacherTimeArr.push(timeIndex);
            }
        } else {
            if (teacherTimeArr.contains(timeIndex)) {
                teacherTimeArr.remove(timeIndex);
            }
        }
    }
}

/*转换成后台需要的json串*/
function toTeacherString() {
    var array = new Array();
    teacherRuleMap.forEach(function (key, valueArr) {
        var rule = {};
        rule["id"] = key;
        var temp = $.map(valueArr, function (value, index) {
            return [value];
        });
        if (temp != null && temp.length > 0) {
            rule["time"] = temp.toString();
            rule["type"] = "-";
            array.push(rule);
        }
    })
    return JSON.stringify(array);
}

/*点击翻页*/
function gotoPage_teacher(mark) {
    //加入分页
    if (mark == 0) {
        //上一页
        if (currentPage_teacher > 1) {
            currentPage_teacher = currentPage_teacher - 1;
        } else {
            alertInfo("当前已经是第一页！");
            return;
        }
    } else {
        //下一页
        if (currentPage_teacher < parseInt($("#totalPage_teacher").text())) {
            currentPage_teacher = currentPage_teacher + 1;
        } else {
            alertInfo("当前已经是最后一页！");
            return;
        }
    }
    $("#currentPage_teacher").text(currentPage_teacher);
    setTeacherTable();
}